$(document).ready(function() {


    /**
     * 时间 显示 隐藏
     */
    // 显示
    $('#datediv').click(function () {
        $('#date-div').show(100);
    });
    //隐藏
    $('.RangeDate_done').on('click',function () {
        $('#date-div').hide(100);
        $("input[name='pageNo']").val(1)
        $("#searchfrom").submit();

    });

    /**
     * 排序 显示 隐藏
     */
    //显示
    $("#sortdiv").click(function () {
        $('#sort-div').show(100);
    });
    //隐藏
    $('#sort-div li').click(function () {
        $(this).siblings('li').removeClass('liorange');
        $(this).addClass('liorange');
        var radioId=$(this).attr("radioid");
        $(radioId).prop("checked",true);
        $('#sort-div').hide(100);
        //提交
        $("input[name='pageNo']").val(1)
        $("#searchfrom").submit();
    });

    /**
     * 筛选 显示 隐藏
     */
    //显示
    $("#criteriadiv").click(function () {
        $('#criteria-div').show(100);
    });
    //隐藏
    $(".c-header ul li:eq(1)").click(function () {
        $("#criteria-div").hide(100);
        $("input[name='pageNo']").val(1)
        $("#searchfrom").submit();
    });

    //清空 筛选条件
    $(".c-header ul li:eq(0)").click(function () {
       /* for(var i=0;$("i")){

        }*/
        $("input[name='lease']").prop("checked",false);
        $("input[name='houseType']").prop("checked",false);
        $(".shaixuan span").removeClass("span-select");
        $("#s1").prop("checked",true);

        slider.reset();


    });


    /**
     *价格滑块
     */
    //

    $("#slider-range").ionRangeSlider({
        min: 0, //最小值
        max: 6, //最大值
        from: 0, //默认从哪个值开始选
        to: 6, //默认选到哪个值
        type: 'double', //设置类型
        grid:true,
        values: [0, 200, 300, 400, 500,800,"不限"],
        step: 1,
        prettify: true,
        hasGrid: true,
        min_interval:1,
        onChange: function (data) {
            $('input[name="startPrice"]').val(data.from_value);
            if(data.to_value=="不限"){
                $('input[name="endPrice"]').val(0);
            }else{
                $('input[name="endPrice"]').val(data.to_value);
            }
            console.log( $('input[name="startPrice"]').val()+"  "+$('input[name="endPrice"]').val())
        },
        onUpdate: function (data) {
            $('input[name="startPrice"]').val(data.from_value);
            if(data.to_value=="不限"){
                $('input[name="endPrice"]').val(0);
            }else{
                $('input[name="endPrice"]').val(data.to_value);
            }
            console.log( $('input[name="startPrice"]').val()+"  "+$('input[name="endPrice"]').val())
        }
    });
    var slider = $("#slider-range").data("ionRangeSlider");
    // 使用`update`指令更新参数

    slider.update({
        from: 0, //默认从哪个值开始选
        to: 6//默认选到哪个值
    });
// 使用reset()重置



    /**
     *  户型选择 出租选择
     */

    $(".shaixuan span").click(function () {

        if($(this).hasClass("span-select")){
            $(this).removeClass("span-select");
            var iputid=$(this).attr("iputid");
            $(iputid).prop("checked",false);

        }else{
            $(this).addClass("span-select");
            var iputid=$(this).attr("iputid");
            $(iputid).prop("checked",true);
        }
       for(var i=0;i<$("input[name='lease']").length;i++){
            console.log($("input[name='lease']")[i].checked+" "+$("input[name='lease']")[i].id)
        }
        for(var i=0;i<$("input[name='houseType']").length;i++){
            console.log($("input[name='houseType']")[i].checked+" "+$("input[name='houseType']")[i].id)
        }
    });

    /**
     * 城市 显示 隐藏
     */
    //显示
    $("input[name='cityName']").click(function(){
        $('#searchbox').css("display","block");
    });
    //取消
    $('#cityclose').click(function () {
        $('#searchbox').css("display","none");
    });

    //选中城市搜索
    $("#place_list li[codeid]").click(function () {
        $("input[name='cityName']").val($(this).text().trim());
        $("input[name='codeId']").val($(this).attr("codeid"));
        $('#searchbox').css("z-index","-100");
        //清除条件
        $("input[name='lease']").prop("checked",false);
        $("input[name='houseType']").prop("checked",false);
        $(".shaixuan span").removeClass("span-select");
        $("input[name='startDate']").val("");
        $("input[name='endDate']").val("");
        $("input[name='pageNo']").val(1)
        slider.reset();
        $("#searchfrom").submit();
    });

    /**
     * 城市选择模块
     */
    //location的height
    $('.location').css({
        'height': $(window).height()
    })
    mui.init();
    mui.ready(function() {
        var list = document.getElementById('list');
        //calc hieght
        list.style.height =  $(window).height() + 'px';
        console.log(document.offsetHeight)
        //create
        window.indexedList = new mui.IndexedList(list);
        $('.mui-placeholder').css({
            'z-index': '-1'
        });
    });
    function other_cities1() {
        //显示地图
        $('#location').show();
        $('#location_box').hide();
        $('#index_page').hide();
        //初始化再次加载
        mui.init();
        mui.ready(function() {
            var list = document.getElementById('list');
            //calc hieght
            list.style.height =  $(window).height() + 'px';
            //create
            window.indexedList = new mui.IndexedList(list);
            $('.mui-placeholder').css({
                'z-index': '-1'
            });
        });
        //获取高度作出padding值
        var guild = $('.guild').height();
        console.log(guild);
        $('.location .mui-indexed-list-inner').css({
            'padding-top': guild
        });
        $('.mui-indexed-list-bar a').height('3.85%');
    }
    //获取高度作出padding值
    var guild = $('.guild').height();
    console.log(guild);
    $('.location .mui-indexed-list-inner').css({
        'padding-top': guild
    });

    $('#searchbox').css("display","none");

    /**
     * 滑动 加载 房子
     */
    $(window).scroll(function () {
        //页面可视区域高度
        var winH = $(window).height();
        var pageH = $(document.body).height();
        console.log(pageH);
        //滚动条top
        var scrollT = $(window).scrollTop();
        //0.02是个参数
        console.log((pageH - winH - scrollT) / winH)
        if((pageH - winH - scrollT) / winH <0.02){
            if(checke){
                console.log("a");
                getHouseList();
            }
        }
    });


    var checke=true;

    /**
     * 加载房子方法
     */
    function getHouseList() {
        checke=false;
        var loading=$("<div class=\"list clearfloat fl box-s\" id='kloading'><div class=\"spinner\"><div class=\"bounce1\"></div>"+
            "<div class=\"bounce2\"></div> <div class=\"bounce3\"></div> </div></div>");
       var from=$("#searchfrom").serialize();
        if(!hasNextPage){
            if($("#no-house").length==0){
                var divHouseInfo=$("<div class=\"list clearfloat fl box-s\" ><p align='center' style='margin: 10px 0px'>沒有更多的房子信息了</p></div>");
                $(".content").append(divHouseInfo);
            }
            return;
        }
        $(".content").append(loading);
        $.ajax({
            url:"../houses/houseInfo",
            data:from,
            type:"GET",
            dataType:"json",
            success:function(result){
                //查询到的数据总数
                var houseInfo=result.extend.pageInfo.list
                console.log(houseInfo.length)
                console.log(houseInfo.length)
/*                if(houseInfo.length==0){
                    var divHouseInfo=$("<div class=\"list clearfloat fl box-s\" ><p align='center' style='margin: 10px 0px'>沒有更多的房子信</p></div>");
                    $(".content").append(divHouseInfo);
                    return;
                }*/
                var pageNo=result.extend.pageInfo.nextPage;
                console.log(pageNo);
                var pageHasNext=result.extend.pageInfo.hasNextPage;
                if(pageHasNext){
                    $("input[name='pageNo']").val(pageNo);
                }else{
                    hasNextPage=false;
                }
                var houseInf=result.extend.pageInfo.list;
                var divListarr=new Array()
                $.each(houseInf,function(index,item) {
                    var divList = $("<div class=\"list clearfloat fl box-s\"></div>");
                    var a = $("<a href=\"../houses/houseDetails/"+item.houseId+"\">");
                    var divImg = $("<div class=\"tu clearfloat\"><span></span><img src=\"../statics/upload/"+item.imgName.split(',')[0]+"\"/></div>");
                    var divRight = $("<div class=\"right clearfloat\"></div>");
                    var divTitPrice = $("<div class=\"tit clearfloat\"></div>");
                    var pTitle = $("<p class=\"fl\">"+item.title+"</p>");
                    var spanPrice = $("<span class=\"fr\" style='font-size: 1.2rem'>"+item.price+"<samp style='font-size: 1.2rem'>元/月</samp></span>");
                    var pHType = $("<p class=\"recom-jianjie\" style='font-size: 1.2rem'>"+item.shi+"室"+item.ting+"厅"+item.wei+"卫|  "+item.area+"m²  | "+item.ftypeName+"</p>");
                    var divrecombottom = $("<div class=\"recom-bottom clearfloat\">" +
                        "<span style='font-size: 1.2rem'><i class=\"iconfont icon-duihao\"></i>随时住</span>" +
                        "<span style='font-size: 1.2rem'><i class=\"iconfont icon-duihao\"></i>家电齐全</span></div>");
                    divTitPrice.append(pTitle).append(spanPrice);
                    divRight.append(divTitPrice).append(pHType).append(divrecombottom);
                    a.append(divImg).append(divRight);
                    divList.append(a);
                    divListarr[index]=divList;

                });
                setTimeout(function () {
                    $("#kloading").remove();
                    for(var item in divListarr){
                        $(".content").append(divListarr[item]);
                    }
                    checke=true;
                },500);
            }

        });

    }

    if($("#main").height()<=$(window).height()){
        $("#main").css("min-height",$(window).height()+2);
    }
   /* var sort="${houseCondition.sort}";
    var lease="${houseCondition.lease}";
    var houseType="${houseCondition.houseType}";
    var startPrice="${houseCondition.startPrice}";
    var endPrice="${houseCondition.endPrice}";*/

    if(sort!=""){
        $("input[name='sort'][value="+sort.trim()+"]").prop("checked",true);
        var  id=$("input[name='sort'][value="+sort.trim()+"]").attr("id");
        $("#sort-div li[radioId='"+"#"+id+"']").siblings('li').removeClass('liorange');
        $("#sort-div li[radioId='"+"#"+id+"']").addClass("liorange");


    }
    if(lease!=""){
       var leases= lease.split(",");
       for(var i=0;i<leases.length;i++){
           $("input[name='lease'][value="+leases[i].trim()+"]").prop("checked",true);
           var  id=  $("input[name='lease'][value="+leases[i].trim()+"]").attr("id");
           $(".shaixuan span[iputid='"+"#"+id+"']").addClass("span-select")
       }
    }
    if(houseType!=""){
        var houseTypes= houseType.split(",");
        for(var i=0;i<houseTypes.length;i++){
            $("input[name='houseType'][value="+houseTypes[i].trim()+"]").prop("checked",true);
            var  id=$("input[name='houseType'][value="+houseTypes[i].trim()+"]").attr("id");
            $(".shaixuan span[iputid='"+"#"+id+"']").addClass("span-select");
        }
    }
    var priceArrs=new Array(0, 200, 300, 400, 500,800,0);
    var from=0;
    var to=6;
    if(startPrice!=""&&endPrice!=""){
        for(var i=0;i<priceArrs.length;i++){
            if(priceArrs[i]==startPrice.trim()&&i!=priceArrs.length-1){
                from=i;
            }
            if(priceArrs[i]==endPrice.trim()&&i!=0){
                to=i;
            }
        }
    }

    var slider2= $("#slider-range").data("ionRangeSlider");
    // 使用`update`指令更新参数
    slider2.update({
        from: from, //默认从哪个值开始选
        to: to //默认选到哪个值
    });
    slider2.reset();
})